package com.violet.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 类说明: 生成jwt 生成rsa的公钥 私钥
 *
 * @author wqf
 * @date 2023/7/10 17:13
 */
public class JWTKeyPairGenerator {

    private static final Logger log = LoggerFactory.getLogger(JWTKeyPairGenerator.class);

    public static void main(String[] args) {
        try {
            // 选择RSA算法
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            // 设置密钥长度
            keyPairGenerator.initialize(2048);
            // 生成密钥对
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            // 获取公钥和私钥
            String publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
            String privateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());
            // 打印公钥和私钥
            System.out.println("Public Key: " + publicKey);
            System.out.println("Private Key: " + privateKey);
        } catch (NoSuchAlgorithmException e) {
            log.error("生成jwt 生成rsa的公钥 私钥失败", e);
        }
    }
}